//Nordic-Sauna JavaScript Functions

function check_user_registration_form() {
    errors = false;
    errorMsg = "";
    errorCount = 0;
    form = document.registrationForm;

    // basic check required fields
    if (form.Username.value == "" || form.Username.length < 3)
    {
        errorMsg += "Please provide a username.\n";
        errors = true;
        if (errorCount == 0) form.Username.focus();
        errorCount++;
    }
    if (form.Password.value == "")
    {
        errorMsg += "Please provide a password.\n";
        errors = true;
        if (errorCount == 0) form.PasswordConfirm.focus();
        errorCount++;
    }
    if (form.PasswordConfirm.value == "")
    {
        errorMsg += "Please confirm your password correctly.\n";
        errors = true;
        if (errorCount == 0) form.PasswordConfirm.focus();
        errorCount++;
    }
    if (form.PasswordConfirm.value != form.Password.value)
    {
        errorMsg += "Please confirm your password correctly.\n";
        errors = true;
        if (errorCount == 0) form.Password.focus();
        errorCount++;
    }
    if (form.FirstName.value == "" || form.FirstName.value.length < 3)
    {
        errorMsg += "Please provide a valid first name.\n";
        errors = true;
        if (errorCount == 0) form.FirstName.focus();
        errorCount++;		
    }
    if (form.LastName.value == "" || form.LastName.value.length < 3)
    {
        errorMsg += "Please provide a valid last name.\n";
        errors = true;
        if (errorCount == 0) form.LastName.focus();
        errorCount++;
    }
    if (form.Email.value == "" || form.Email.value.length == 0)
    {
        errorMsg += "Please provide an e-mail address.\n";
        errors = true;
        if (errorCount == 0) form.Email.focus();
        errorCount++;
    }
    else if (form.Email.value != "" || form.Email.value.length != 0)
    {
        /* From: http://www.w3schools.com/js/js_form_validation.asp
        The function below checks if the content has the general syntax of an email.
        This means that the input data must contain at least an @ sign and a dot (.).
        Also, the @ must not be the first character of the email address, and the last dot must at least be one character after the @ sign:
        */

        atPosition = form.Email.value.indexOf("@");
        dotPosition = form.Email.value.lastIndexOf(".");

        if (atPosition < 1 || dotPosition - atPosition < 2 || form.Email.value.length < 6)
        {
            errorMsg += "Please provide a valid e-mail address.\n";
            errors = true;
            if (errorCount == 0) form.Email.focus();
            errorCount++;
        }
    }
    
    if (form.Phone.value == "" || form.Phone.value.length == 0)
    {
        errorMsg += "Please provide a phone nubmer.\n";
        errors = true;
        if (errorCount == 0) form.Phone.focus();
        errorCount++;
    }
        
    // message if errors exist
    if (errors == true)
    {
        alert("You have " + errorCount + " errors.\n" + errorMsg);
        return !errors;
    }	
}

function check_login_form()
{
    errors = false;
    errorMsg = "";
    errorCount = 0;
    form = document.loginForm;
    if (form.username.value == "")
    {
        errorMsg += "Please provide a username.\n";
        errors = true;
        if (errorCount == 0) form.username.focus();
        errorCount++;
    }
    if (form.password.value == "")
    {
        errorMsg += "Please provide a password.\n";
        errors = true;
        if (errorCount == 0) form.password.focus();
        errorCount++;
    }
    // message if errors exist
    if (errors == true)
    {
        alert("You have " + errorCount + " errors.\n" + errorMsg);
        return !errors;
    }
}

function check_software_form()
{
    errors = false;
    errorMsg = "";
    errorCount = 0;
    form = document.updateSoftware;
    if (form.Name.value == "")
    {
        errorMsg += "Please provide a name.\n";
        errors = true;
        if (errorCount == 0) form.Name.focus();
        errorCount++;
    }
    if (form.Description.value == "")
    {
        errorMsg += "Please provide a description.\n";
        errors = true;
        if (errorCount == 0) form.Description.focus();
        errorCount++;
    }
    // message if errors exist
    if (errors == true)
    {
        alert("You have " + errorCount + " errors.\n" + errorMsg);
        return !errors;
    }
}

function check_profile_form()
{
    errors = false;
    errorMsg = "";
    errorCount = 0;
    form = document.updateProfile;

    // basic check required fields
    if (form.FirstName.value == "" || form.FirstName.value.length < 3)
    {
        errorMsg += "Please provide a valid first name.\n";
        errors = true;
        if (errorCount == 0) form.FirstName.focus();
        errorCount++;		
    }
    if (form.LastName.value == "" || form.LastName.value.length < 3)
    {
        errorMsg += "Please provide a valid last name.\n";
        errors = true;
        if (errorCount == 0) form.LastName.focus();
        errorCount++;
    }
    if (form.Email.value == "" || form.Email.value.length == 0)
    {
        errorMsg += "Please provide an e-mail address.\n";
        errors = true;
        if (errorCount == 0) form.Email.focus();
        errorCount++;
    }
    else if (form.Email.value != "" || form.Email.value.length != 0)
    {
        /* From: http://www.w3schools.com/js/js_form_validation.asp
        The function below checks if the content has the general syntax of an email.
        This means that the input data must contain at least an @ sign and a dot (.).
        Also, the @ must not be the first character of the email address, and the last dot must at least be one character after the @ sign:
        */

        atPosition = form.Email.value.indexOf("@");
        dotPosition = form.Email.value.lastIndexOf(".");

        if (atPosition < 1 || dotPosition - atPosition < 2 || form.Email.value.length < 6)
        {
            errorMsg += "Please provide a valid e-mail address.\n";
            errors = true;
            if (errorCount == 0) form.Email.focus();
            errorCount++;
        }
    }
    
    if (form.Phone.value == "" || form.Phone.value.length == 0)
    {
        errorMsg += "Please provide a phone nubmer.\n";
        errors = true;
        if (errorCount == 0) form.Phone.focus();
        errorCount++;
    }
        
    // message if errors exist
    if (errors == true)
    {
        alert("You have " + errorCount + " errors.\n" + errorMsg);
        return !errors;
    }
}